package com.cs.disposition.mqtt;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Service;

@Service
public class MqttDefaultCallbackService implements MqttCallback {
    private final MqttMessageHandler messageHandler;

    public MqttDefaultCallbackService(MqttMessageHandler messageHandler) {
        this.messageHandler = messageHandler;
    }

    @Override
    public void connectionLost(Throwable cause) {
        System.out.println("Connection lost: " + cause.getMessage());
    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        // 使用 MqttMessageHandler 来处理接收到的消息
        messageHandler.messageArrived(topic, message.getPayload());
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        System.out.println("Delivery complete: " + token.getMessageId());
    }
}
